package com.cy.pj.sys.web.interceptor;

import com.cy.pj.sys.web.advice.GlobalExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;

public class TimeAccessInterceptor implements HandlerInterceptor {

    private static final Logger log =
            LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 后端
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
//        System.out.println("拦截成功");

        LocalTime now = LocalTime.now();
        int hour = now.getHour();

        log.info("hour{}", hour);
        if (hour <= 6 || hour >= 22)
            throw new RuntimeException("请在6~22点进行访问");
                return true;
    }
}